from selenium.webdriver.common.by import By
from base.base_page import BasePage


class SearchPage(BasePage):
    def __init__(self):
        super().__init__()
        self.add_cart_list = (By.LINK_TEXT, "加入购物车")
        self.page_list = (By.CSS_SELECTOR, "ul.pagination>li")
        self.add_frame_loc = (By.CSS_SELECTOR, "[id*=layui-layer-iframe]")
        self.add_tips_loc = (By.CSS_SELECTOR, ".conect-title>span")

    def add_cart(self, goods_index=1, page_index=1):
        if page_index == 1:
            goods = self.find_els(self.add_cart_list)[goods_index-1]
            goods.click()
        else:
            page = self.find_els(self.page_list)[page_index-1]
            page.click()
            goods = self.find_els(self.add_cart_list)[goods_index-1]
            goods.click()

    def get_add_cart_tips(self):
        self.switch_to_iframe(self.add_frame_loc)
        tips = self.find_el(self.add_tips_loc).text
        self.driver.switch_to.default_content()
        return tips
